package org.example.car.system.controller;

import com.github.pagehelper.PageInfo;
import org.example.car.system.config.ExcelDownloader;
import org.example.car.system.config.R;
import org.example.car.system.domain.SysPost;
import org.example.car.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("wei")
@CrossOrigin
public class WeiController {

    @Autowired
    SysPostService sysPostService;

    //岗位列表
    @RequestMapping("/postList")
    public List<SysPost> postList(){
        List<SysPost> list = sysPostService.selectAll();
        return list;
    }

    @RequestMapping("typeList")
    public R list(@RequestBody SysPost sysPost){
        System.out.println("================"+sysPost);
        PageInfo<SysPost> list = sysPostService.getlist(sysPost);
        return R.successed(list);

    }

    @RequestMapping("del")
    public R del(Integer postId){

       sysPostService.del(postId);
       return R.successed(null);
    }

    @RequestMapping("update")
    public R update(@RequestBody SysPost sysPost){
        sysPost.setUpdateBy("admin");
        sysPost.setUpdateTime(new Date());
        sysPostService.updateById(sysPost);
        return R.successed(null);
    }

    @RequestMapping("deleted")
    public R deleted(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        sysPostService.removeBatchByIds(list);
        return R.successed(null);
    }

    @RequestMapping("add")
    public R add(@RequestBody SysPost sysPost){
        sysPost.setCreateTime(new Date());
        sysPostService.save(sysPost);
        return R.successed(null);
    }

    @RequestMapping("excel")
    public void excel(HttpServletResponse response) throws IOException {
        List<SysPost> list = sysPostService.list();
         ExcelDownloader.download(response,list,SysPost.class,"岗位");
    }
}
